package top.yxf.springboot.drools.service;

import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.yxf.springboot.drools.entity.Calculation;
import top.yxf.springboot.drools.entity.Student;
import top.yxf.springboot.drools.utils.KieSessionUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xs.wu
 */
@Service
public class RuleService {


    @Autowired
    private KieBase kieBase;

    public void rule() {
        KieSession kieSession = kieBase.newKieSession();
        kieSession.fireAllRules();
        kieSession.dispose();
    }


    /**
     * 个人所得税计算
     *
     * @param calculation
     * @return
     */
    public Calculation calculate(Calculation calculation) {
        KieSession kieSession = kieBase.newKieSession();
        kieSession.insert(calculation);
        kieSession.fireAllRules();
        kieSession.dispose();
        return calculation;
    }


    public List<String> studentInfoCheck(Student student) throws Exception {
        // 编写规则
        String drl = "package testStudent\n" +
                "import top.yxf.springboot.drools.entity.Student\n" +
                "\n" +
                "global java.util.List listRules\n" +
                "\n" +
                "rule \"student_age_1\"\n" +
                "    when\n" +
                "        $stu:Student(age > 17 && name != null);\n" +
                "    then\n" +
                "        System.out.println(\"student_age_1 and Student = \" + $stu);\n" +
                "        listRules.add($stu.getName());\n" +
                "end";
        KieSession session = KieSessionUtil.getKieSessionFromXLS(drl);
//        session.getAgenda().getAgendaGroup("sign").setFocus();
        session.insert(student);
        List<String> listRules = new ArrayList<>();
        session.setGlobal("listRules", listRules);

        session.fireAllRules();

        return listRules;
    }


}
